package com.demo.java.pattern.stratege;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.lang.annotation.Annotation;

/**
 * -Dsun.misc.ProxyGenerator.saveGeneratedFiles=true
 *
 * System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "proxys");
 */

@Data
@AllArgsConstructor
public class OrderHandlerTypeImpl implements OrderHandlerType {
    private String source;
    private String payment;

    public OrderHandlerTypeImpl(Order order) {
        this.source = order.getSource();
        this.payment = order.getPayment();
    }

    /**
     * 注解类型实例（被声明注解的类）存储到map, 然后通过属性值相同的其他实例来获取，重写hashCode和equals是关键
     */
    public int hashCode() {
        int hashCode = 0;
        hashCode += (127 * "source".hashCode()) ^ source.hashCode();
        hashCode += (127 * "payment".hashCode()) ^ payment.hashCode();
        return hashCode;
    }

    public boolean equals(Object obj) {
        if (!(obj instanceof OrderHandlerType)) {
            return false;
        }
        OrderHandlerType other = (OrderHandlerType) obj;
        return this.source.equals(other.source()) && this.payment.equals(other.payment());
    }

    @Override
    public String source() {
        return this.source;
    }

    @Override
    public String payment() {
        return this.payment;
    }

    @Override
    public Class<? extends Annotation> annotationType() {
        return OrderHandlerType.class;
    }
}
